<template>
  <!-- 登陆页面 -->
  <div id="login" class="login_container">

    <div class="login_box">

      <!-- 头像区域 -->
      <div class="avatar_box">
        <img class="login_img" src="@/assets/logo.png" alt="" />
      </div>
      <!-- 登录表单区域 -->
      <el-form class="login_form" ref="loginForm" :model="loginForm" :rules="loginFormRules" @input="change($event)" label-width="100px">
        <h3 class="login_title">采购订单查询系统</h3>
        <el-form-item prop="username">
          <el-input v-model="loginForm.userid" clearable prefix-icon="el-icon-user" placeholder="请输入用户名"></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input type="password" v-model="loginForm.password" clearable prefix-icon="el-icon-lock" show-password placeholder="请输入密码"></el-input>
        </el-form-item>

        <el-form-item class="form_savePWD">
          <el-checkbox>保存密码</el-checkbox>
        </el-form-item>

        <el-form-item class="form_button">
          <el-button type="primary" @click="submitForm">登录</el-button>
          <el-button type="info" @click="resetLoginForm" plain>重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import Cookies from "js-cookie";
import request from "../utils/request.js"; // axios请求工具类
export default {
  data() {
    return {
      // 登录表单数据绑定对象
      loginForm: {
        userid: "admin", // 用户名
        password: "123456", // 密码
      },
      // 表单验证规则
      loginFormRules: {
        // 验证用户名是否合法
        userid: [
          { required: true, message: "请输入账号", trigger: "blur" },
          {
            min: 5,
            max: 20,
            message: "长度在 5 到 20 个字符",
            trigger: "blur",
          },
        ],
        // 验证密码是否合法
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 5,
            max: 20,
            message: "长度在 5 到 20 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    submitForm() {
      // 登陆验证
      this.$refs.loginForm.validate(async (valid) => {
        if (!valid) {
          this.$message({
            showClose: true,
            message: "用户名或密码不正确",
            type: "error",
            center: true,
            duration: 1000,
          });
          return;
        }
        if (valid) {
          const loading = this.$loading({
            lock: true,
            text: "正在登陆",
            spinner: "el-icon-loading",
            background: "rgba(255,255,255,0.9)",
          });

          // 登陆请求
          request.post("/api/user/login", this.loginForm).then(
            (res) => {
              if (res.code === 0) {
                this.$message({
                  showClose: true,
                  message: "欢迎你" + this.loginForm.userid,
                  type: "success",
                  center: true,
                  duration: 1000,
                });
                // Cookies.set("username", this.loginForm.username);
                // window.sessionStorage.setItem("token", result.data.token);
                this.$router.push("/purchase"); // 登陆成功跳转主页
              } else {
                this.$message.error({
                  showClose: true,
                  message: "登陆失败</br>" + res.msg,
                  dangerouslyUseHTMLString: true,
                  duration: 1000,
                });
                this.$router.push("/404"); // 失败页面
              }
            },
            (error) => {
              this.$message.error({
                showClose: true,
                message: "接口错误" + error,
                dangerouslyUseHTMLString: true,
                duration: 1000,
              });
              this.$router.push("/404"); // 失败页面
            }
          );
          loading.close();
          // console.log(result.data.token)
        }
      });
    },
    // 点击重置按钮重置登录表单
    resetLoginForm() {
      // console.log(this)
      this.$refs.loginForm.resetFields();
    },
  },
};
</script>

<style scoped>
.login_container {
  background-color: #2b4b6b;
  padding-top: 50%;
}

.login_box {
  width: 450px;
  height: 350px;
  background-color: #fff;
  border: 1px solid #eee;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.avatar_box {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #eee;
  position: relative;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
}
.login_img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: #eee;
}

.login_form {
  /* width: 80%; */
  transform: translate(-10%, -30%);
}

.login_title {
  width: 300px;
  height: 50px;
  color: #2b4b6b;
  /* background-color: #fff; */
  font-size: 20px;
  transform: translate(64%, 0%);
}

.form_button {
  transform: translate(40%, -150%);
}

.form_savePWD {
  transform: translate(0%, -20%);
}
</style>
